package main.java.common.util;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang.StringUtils;

public class DateUtil {
	private final static SimpleDateFormat sdYear = new SimpleDateFormat("yyyy");
	private final static SimpleDateFormat sdDay = new SimpleDateFormat("yyyy-MM-dd");
	private final static SimpleDateFormat sdDays = new SimpleDateFormat("yyyyMMdd");
	private final static SimpleDateFormat sdTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	public static String getYear() {
		return sdYear.format(new Date());
	}
	
	Timestamp timestamp = new Timestamp(new Date().getTime());
	
	public static String getDay() {
		return sdDay.format(new Date());
	}
	
	public static Date fomatDate(String s) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		try {
			return df.parse(s);		
			}catch(Exception e) {
				e.printStackTrace();
				return null;
			}
	}
	
	/**
	 * 日期比较 若s>=e ture else false
	 * @param s
	 * @param e
	 * @return
	 */
	public static boolean compareDate(String s, String e) {
		if(fomatDate(s) == null || fomatDate(e) == null) {
			return false;
		}
		return fomatDate(s).getTime() >= fomatDate(e).getTime();
	}
	
	/*
	 * 检查日期是否合法
	 */
	public static boolean isValidDate(String s) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		try {
			df.parse(s);
			return true;
		}catch(Exception e) {
			return false;
		}
	}
	
	/*
	 * 年数差
	 */
	public static int getDifYear(String s,String e) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		try {
			int year = (int)((df.parse(s).getTime() - df.parse(e).getTime())/(24 * 60 * 60 * 1000 * 365));
			return year;
		}catch(Exception ex) {
			return 0;
		}
	}
	
	/*
	 * 天数差
	 */
	public static long getSubDays(String begin, String end) {
		long day = 0;
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		try {
			long beginDate = df.parse(begin).getTime();
			long endDate = df.parse(end).getTime();
			day = (beginDate - endDate)/(24 * 60 * 60 * 1000);
		}catch(Exception e) {
			e.printStackTrace();
		}
		return day;
	}
	
	/*
	 * n天之后的日期
	 */
	public static String getAfterDay(String day) {
		int n = Integer.parseInt(day);
		Calendar can = Calendar.getInstance();
		can.add(Calendar.DATE, n);
		Date date = can.getTime();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return df.format(date);
	}
	
	/*
	 * n天之前的日期
	 */
	public static String getBeforeDay(String day) {
		int n = Integer.parseInt(day);
		Calendar can = Calendar.getInstance();
		can.add(Calendar.DATE, n);
		Date date = can.getTime();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return df.format(date);
	}
	
	/*
	 * 得到n天后的星期数
	 */
	public static String getAfterDayWeek(String day) {
		int n = Integer.parseInt(day);
		Calendar can = Calendar.getInstance();
		can.add(Calendar.DATE, n);
		Date date = can.getTime();
		DateFormat df = new SimpleDateFormat("E");
		return df.format(date);
	}
	
	public static String dateFormat(String format, Date date) {
		if(format == "") {
			format = "yyyy-MM-dd";
		}
		if(date != null) {
			try {
				DateFormat df = new SimpleDateFormat(format);
				return df.format(date);
			}catch(Exception e) {
				e.printStackTrace();
			}
		}
		return "";
	}
	
	public static String dateFormatHH(Date date) {
		return dateFormat("yyyy-MM-dd HH:mm:ss", date);
	}
	
	public static Date parseDate(String date) {
		if(StringUtils.isNotBlank(date)) {
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			try {
				return df.parse(date);
			}catch(Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	/**
	 * 相差时间
	 */
	public static String getTimes(String strDate) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			Date date = df.parse(strDate);
			Date now = new Date();
			long times = now.getTime() - date.getTime();
			long day = times / (24 * 60 * 60 * 1000);
			long hour = times / (60 * 60 * 1000);
			long min = times / (60 * 1000);
			long sec = times / 1000;
			
			if(day > 0) {
				return day + "天前";
			} else if(hour > 0) {
				return hour + "小时前";
			} else if(min > 0) {
				return min + "分钟前";
			} else {
				return sec + "秒前";
			}
		}catch(Exception e) {
			e.printStackTrace();
		}
		return "";
	}
	
	public static String getTimestamp() {
		return String.valueOf(System.currentTimeMillis() / 1000);
	}
}
